﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.Data.Entity;
using TomNet.IMSC.Web.Areas.LowPriceHouse.Models;
using TomNet.IMSC.Web.Common;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.Security;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Areas.LowPriceHouse.Controllers
{
    public class LowPriceHouseReportController : BaseController
    {

        private DefaultDbContext DbContext = new DefaultDbContext();




        //总体统计===================================================================================

        [Description("总体统计")]
        //[GlobalAuthorization]
        public ActionResult LowPriceHouse4TotalIndex(int GroupID = 1)
        {
            ViewBag.GroupID = GroupID;
            return View();
        }

        [Description("总体统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult LowPriceHouse4TotalIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate, int GroupID = 1)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);
            
            //访问数据库函数
            var query = DbContext.SqlQuery<LowPriceHouse4TotalListModel>("select * from [dbo].[Fn_Table_Report_LowPriceHouse4Total](@p0,@p1,@p2,@p3)", loginModel.Id, beginDate, endDate, GroupID);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<LowPriceHouse4TotalListModel>().AsQueryable();// null;
            

            //此处可以采用匿名对象 GridData<object>
            GridData<LowPriceHouse4TotalListModel> gridData = new GridData<LowPriceHouse4TotalListModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //中介统计===================================================================================

        [Description("中介统计")]
        //[GlobalAuthorization]
        public ActionResult LowPriceHouse4IntermediariesIndex(int GroupID = 1)
        {
            ViewBag.GroupID = GroupID;
            return View();
        }

        [Description("中介统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult LowPriceHouse4IntermediariesIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate, int GroupID = 1)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            var query = DbContext.SqlQuery<LowPriceHouse4IntermediariesListModel>("select * from [dbo].[Fn_Table_Report_LowPriceHouse4Intermediaries](@p0,@p1,@p2,@p3)", loginModel.Id, beginDate, endDate, GroupID);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<LowPriceHouse4IntermediariesListModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<LowPriceHouse4IntermediariesListModel> gridData = new GridData<LowPriceHouse4IntermediariesListModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //房主统计===================================================================================

        [Description("房主统计")]
        //[GlobalAuthorization]
        public ActionResult LowPriceHouse4OwnersIndex(int GroupID = 1)
        {
            ViewBag.GroupID = GroupID;
            return View();
        }

        [Description("房主统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult LowPriceHouse4OwnersIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate, int GroupID = 1)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            var query = DbContext.SqlQuery<LowPriceHouse4OwnersListModel>("select * from [dbo].[Fn_Table_Report_LowPriceHouse4Owners](@p0,@p1,@p2,@p3)", loginModel.Id, beginDate, endDate, GroupID);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<LowPriceHouse4OwnersListModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<LowPriceHouse4OwnersListModel> gridData = new GridData<LowPriceHouse4OwnersListModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //渠道人员统计===================================================================================

        [Description("渠道人员统计")]
        //[GlobalAuthorization]
        public ActionResult LowPriceHouse4OriginStaffIndex(int GroupID = 1)
        {
            ViewBag.GroupID = GroupID;
            return View();
        }

        [Description("渠道人员统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult LowPriceHouse4OriginStaffIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate, int GroupID = 1)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            var query = DbContext.SqlQuery<LowPriceHouse4OriginStaffListModel>("select * from [dbo].[Fn_Table_Report_LowPriceHouse4OriginStaff](@p0,@p1,@p2,@p3)", loginModel.Id, beginDate, endDate, GroupID);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<LowPriceHouse4OriginStaffListModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<LowPriceHouse4OriginStaffListModel> gridData = new GridData<LowPriceHouse4OriginStaffListModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //销售人员统计===================================================================================

        [Description("销售人员统计")]
        //[GlobalAuthorization]
        public ActionResult LowPriceHouse4SellStaffIndex(int GroupID = 1)
        {
            ViewBag.GroupID = GroupID;
            return View();
        }

        [Description("销售人员统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult LowPriceHouse4SellStaffIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate, int GroupID = 1)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);

            //访问数据库函数
            var query = DbContext.SqlQuery<LowPriceHouse4SellStaffListModel>("select * from [dbo].[Fn_Table_Report_LowPriceHouse4SellStaff](@p0,@p1,@p2,@p3)", loginModel.Id, beginDate, endDate, GroupID);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<LowPriceHouse4SellStaffListModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<LowPriceHouse4SellStaffListModel> gridData = new GridData<LowPriceHouse4SellStaffListModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        //维护人员统计===================================================================================

        [Description("维护人员统计")]
        //[GlobalAuthorization]
        public ActionResult LowPriceHouse4StationedStaffIndex(int GroupID = 1)
        {
            ViewBag.GroupID = GroupID;
            return View();
        }

        [Description("维护人员统计")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult LowPriceHouse4StationedStaffIndexAsync(int pageIndex, int pageSize, DateTime beginDate, DateTime endDate, int GroupID = 1)
        {
            //获取权限
            var cookies = CookiesManagement.GetCookies();
            var ticket = CookiesManagement.GetTicket(cookies);
            var loginModel = CookiesManagement.GetLoginModel(ticket);
            UpdateLoginModelAuthorityIds(loginModel);

            //时间
            beginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, 0, 0, 0);
            endDate = new DateTime(endDate.AddDays(1).Year, endDate.AddDays(1).Month, endDate.AddDays(1).Day, 0, 0, 0);
            
            //访问数据库函数
            var query = DbContext.SqlQuery<LowPriceHouse4StationedStaffListModel>("select * from [dbo].[Fn_Table_Report_LowPriceHouse4StationedStaff](@p0,@p1,@p2,@p3)", loginModel.Id, beginDate, endDate, GroupID);

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingNumber);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<LowPriceHouse4StationedStaffListModel>().AsQueryable();// null;


            //此处可以采用匿名对象 GridData<object>
            GridData<LowPriceHouse4StationedStaffListModel> gridData = new GridData<LowPriceHouse4StationedStaffListModel>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm");
        }

        

        
        




    }
}